package cn.net.isir.service;

import cn.net.isir.core.BasicService;
import cn.net.isir.core.SetBasicDao;
import cn.net.isir.dao.UserDao;
import cn.net.isir.dao.UserInfoDao;
import cn.net.isir.dao.UserOrgDao;
import cn.net.isir.entity.Organizations;
import cn.net.isir.entity.UserInfo;
import cn.net.isir.entity.UserOrg;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserOrgService extends BasicService<UserOrg> {
    @SetBasicDao
    @Resource
    private UserOrgDao userOrgDao;
    @Resource
    private UserDao userDao;
    @Resource
    private UserInfoDao userInfoDao;

    /**
     * 根据用户id，查询组织机构关联表
     *
     * @param userId userid
     * @return 关联实体类
     */
    public UserOrg selectByUserId(String userId) {
        UserOrg userOrg = new UserOrg();
        userOrg.setUserId(userId);
        return userOrgDao.selectOne(userOrg);
    }

    /**
     * 根据组织机构列表删除用户
     * @param subList 子列表
     * @return 删除结果
     */
    public void deleteUserByOrgList(List<Organizations> subList) {
        for (Organizations org : subList) {
            String orgId = org.getId();
            UserOrg userOrg = new UserOrg();
            userOrg.setOrgId(orgId);
            List<UserOrg> userOrgList = userOrgDao.selectList(userOrg);
            for (UserOrg o:userOrgList){
                String userId = o.getUserId();
                userDao.delete(userId);
                userInfoDao.delete(userId);
            }
        }
    }
}
